package views.admin;

import tools.Icons;
import utils.DBUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.Statement;

public class AddAdminFrame extends JFrame {
    private JPanel panBtn = new JPanel();
    private JPanel panLab = new JPanel();
    private JLabel jL_account = new JLabel("账  号：");
    private JLabel jL_password = new JLabel("密    码：");
    private JLabel jL_repeatpass = new JLabel("确认密码：");
    private JTextField jT_account = new JTextField();
    private JPasswordField jP_pass = new JPasswordField();
    private JPasswordField jP_repeatPass = new JPasswordField();
    private JButton btn_add = new JButton("添加");
    private JButton btn_repeat = new JButton("重置");
    private JLabel jL_title = new JLabel();
    ImageIcon imageIcon = new ImageIcon("src/images/admin.png");
    Icons icons = new Icons();

    public AddAdminFrame() {
        setTitle("添加管理员");
        setSize(400, 300);
        setResizable(false); // 不可改变窗口大小
        setIconImage(imageIcon.getImage().getScaledInstance(80, 80, Image.SCALE_DEFAULT));
        // 获取屏幕大小和当前frame的大小
        Dimension thisScreen = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension thisFrame = this.getSize();
        // 使启动窗口位于屏幕的正中心
        setLocation((thisScreen.width - thisFrame.width) / 2,
                (thisScreen.height - thisFrame.height) / 2);
        // 设置单击窗口的【关闭】按钮时将发生相应的动作
        setDefaultCloseOperation(HIDE_ON_CLOSE);

        jL_title.setFont(new Font("TsangerYuMo W03", Font.BOLD, 26));
        jL_title.setHorizontalAlignment(JLabel.CENTER);
        jL_title.setForeground(new Color(255, 51, 51));
        jL_title.setText("添  加  管  理  员");

        jL_account.setSize(100, 80);
        jL_account.setLocation(50, 20);
        jL_account.setHorizontalAlignment(JLabel.RIGHT);

        jL_password.setSize(100, 80);
        jL_password.setLocation(50, 50);
        jL_password.setHorizontalAlignment(JLabel.RIGHT);

        jL_repeatpass.setSize(100, 80);
        jL_repeatpass.setLocation(50, 80);
        jL_repeatpass.setHorizontalAlignment(JLabel.RIGHT);

        jT_account.setSize(150, 25);
        jT_account.setLocation(160, 50);

        jP_pass.setSize(150, 25);
        jP_pass.setLocation(160, 80);

        jP_repeatPass.setSize(150, 25);
        jP_repeatPass.setLocation(160, 110);

        panBtn.add(btn_add);
        panBtn.add(btn_repeat);
        panLab.setLayout(null);

        panLab.add(jL_account);
        panLab.add(jL_password);
        panLab.add(jL_repeatpass);


        panLab.add(jT_account);
        panLab.add(jP_pass);
        panLab.add(jP_repeatPass);


        add(jL_title, BorderLayout.NORTH);
        add(panBtn, BorderLayout.SOUTH);
        add(panLab, BorderLayout.CENTER);

        btn_add.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                int account = Integer.parseInt(jT_account.getText().trim());
                String pwd = jP_pass.getText().trim();
                String repeatpass = jP_repeatPass.getText().trim();

                if (pwd.equals(repeatpass)) {
                    Statement stmt = DBUtil.getStatement();
                    String sql = "select account from admin where account='" + account
                            + "';";
                    String sql2 = "insert into admin(account, password) values("
                            + account
                            + ",'"
                            + pwd
                            + "');";
                    try {
                        ResultSet rs = stmt.executeQuery(sql);
                        if (!rs.next()) {
                            stmt.executeUpdate(sql2);
                            JOptionPane.showMessageDialog(null, "添加成功。", "成功", JOptionPane.INFORMATION_MESSAGE, icons.getOkImage());
                            dispose();
                        } else {
                            JOptionPane.showMessageDialog(null, "已存在！", "错误", JOptionPane.ERROR_MESSAGE, icons.getErrorImage());
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "两次输入的密码不一致", "错误", JOptionPane.ERROR_MESSAGE, icons.getErrorImage());
                }
            }
        });
        btn_repeat.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                jT_account.setText("");
                jP_pass.setText("");
                jP_repeatPass.setText("");
            }
        });
    }

    public static void main(String[] args) {
        new AddAdminFrame().setVisible(true);
    }
}
